home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / 'Z' and 'FB' Basic (ML) / TEFind Folder / TEFind.BAS next >
Encoding:
BASIC Source File  |  1992-06-13  |  7.2 KB  |  197 lines  |  [TEXT/ZBAS]

  1.  
  2. ' ZBasic 5.0 and FutureBASIC TEFind Function
  3. ' 68x0 Assembler - Michael Burton,   1991,1992
  4. '                  Michael Lininger, 1991,1992
  5.  
  6.  
  7. DIM T,B,L,R  :' General Box Record Structure
  8.  
  9.  
  10. GOTO "Main_Start"
  11.  
  12.  
  13. LONG FN TEFind(TEFCharPtr&,TEFCharLen&,TEFFindStr$)
  14.  
  15. ' Last Modified (4/20/91)
  16. ' Long Function (© 4/91 • Michael Burton   •)
  17. '               (© 4/91 • Michael Lininger •)
  18.         
  19.     Found% = 0
  20.     TEFFndLen  = LEN(TEFFindStr$)          :' Length of search string
  21.                                           :' HI & LO word of REGISTER D0
  22.     TEFCmpLen& = (TEFFndLen<<16)+TEFFndLen :' must equal length
  23.     TEFCmpPtr& = VARPTR(TEFFindStr$)+1     :' Pointer to search string
  24.                                           :' Loop from start of text to
  25.                                           :' end minus search length
  26.     TEFEnd& = TEFCharLen&-TEFFndLen        :' Bytes to check
  27.                                           :' This sets up the toolbox trap
  28.                                           :' for "_cmpString"
  29.     REGISTER(A0) = TEFCharPtr&             :' Pointer #1 to Addr register 0
  30.     REGISTER(A1) = TEFCmpPtr&              :' Pointer #2 to Addr register 1
  31.     REGISTER(D1) = TEFEnd&                 :' Bytes to check
  32.     REGISTER(D2) = TEFCmpLen&              :' Copied to D0 when needed
  33.     MACHLG &1611                           :'       MOVE.B (A1),D3 ;first char of find str
  34.     MACHLG &0203                           :'       ANDI.B #$1F,D3 ;ignore case
  35.     MACHLG &001F
  36.     MACHLG &1010                           :'@loop: MOVE.B (A0),D0 ;current char
  37.     MACHLG &0200                           :'       ANDI.B #$1F,D0 ;ignore case
  38.     MACHLG &001F
  39.     MACHLG &B600                           :'       CMP.B D0,D3    ;possible match?
  40.     MACHLG &6608                           :'       BNE @next      ; skip if not
  41.     MACHLG &2002                           :'       MOVE.L D2,D0   ;use length
  42.     MACHLG &A03C                           :'       _CmpString
  43.     MACHLG &4A80                           :'       TST.L D0       ;got a match?
  44.     MACHLG &6708                           :'       BEQ @done      ; exit if so
  45.     MACHLG &5288                           :'@next: ADDQ.L #1,A0   ;else point to next
  46.     MACHLG &51C9                           :'       DBRA D1,@loop  ;loop 'til done
  47.     MACHLG &FFEA
  48.     MACHLG &50C0                           :'       ST D0          ;note failure
  49.     MACHLG &4E71                           :'@done: NOP            ;just a placeholder
  50.     X = REGISTER(D0)                       :' D0 = non-zero if different and 0 if same
  51.     TEFPtr& = REGISTER(A0)                 :' Last address checked
  52.     LONG IF X = 0                          :' If match found
  53.         TEFSelStart = TEFPtr&-TEFCharPtr&   :' Start @ current pointer
  54.       TEFSelEnd = TEFPtr&                 :' Select the "found" text
  55.         Found% = 1
  56.     END IF
  57.  
  58. END FN = Found%
  59.  
  60.  
  61. LONG FN getText$(dPtr&,whichItem%,Size%)
  62.     CALL GETDITEM(dPtr&,whichItem%,theType%,theTxtHandle&,T)
  63.     CALL GETITEXT(theTxtHandle&,theText$)
  64.     LONG IF Size% > 0
  65.         theText$ = LEFT$(theText$,Size%)
  66.     END IF
  67. END FN = theText$
  68.  
  69.  
  70. LONG FN setText(dPtr&,whichItem%,theText$)
  71.     CALL GETDITEM(dPtr&,whichItem%,theType%,theTxtHandle&,T)
  72.     CALL SETITEXT(theTxtHandle&,theText$)
  73. END FN = 0
  74.  
  75.  
  76. LONG FN frameDLOGBtn(dPtr&,whichItem%)
  77.     oldPort& = WINDOW(14)                             :' Store the old window grafport so we can restore it
  78.     CALL SETPORT(dPtr&)                               :' Set the current dialog window as the active grafport
  79.     CALL GETDITEM(dPtr&,whichItem%,theType%,theHandle&,T)
  80.     CALL INSETRECT(T,-4,-4)
  81.     CALL PENNORMAL:PEN 3,3
  82.     CALL FRAMEROUNDRECT(T,16,16)
  83.     CALL PENNORMAL
  84.     CALL SETPORT(oldPort&)                            :' Restore the old window grafport as the new active one
  85. END FN = 0
  86.  
  87.  
  88. "Main_Start"
  89.  
  90. ' (Sample 6/92 • Michael Lininger •)
  91.  
  92. ' This demo code shows how to scan a TEXT file for a selected text match.  This
  93. ' can be converted to scan a TE Edit Field for a match, or even to a find and
  94. ' replace.  In this demo we just scan a text file of any size and count the number
  95. ' of matches and print the total to the screen.
  96.  
  97.     DLOGHndl& = FN GETRESOURCE(CVI("DLOG"),8128)      :' Get Handle to DLOG 8128
  98.     LONG IF DLOGHndl& = 0                             :' If Z5, Non Project FB or DLOG 8128 not found
  99.         DLOGFile$ = "COUNTERPoint™:TEFind.RES"         :' Where is the TEFind Resource located
  100.         ResRef% = FN OPENRESFILE(DLOGFile$)            :' Open the Resource with the DLOG 8128
  101.     XELSE
  102.         CALL RELEASERESOURCE(DLOGHndl&)
  103.     END IF
  104.     OSErr% = FN RESERROR                              :' Clear any Resource Errors, Should be 0
  105.  
  106.    ' Get the Text String to search for...
  107.     TEFindStr$ = "Sample Text"
  108.     done% = 0
  109.     DO
  110.         dlogPtr& = FN GETNEWDIALOG(8128,0,-1)
  111.         OSErr% = FN setText(dlogPtr&,3,TEFindStr$)
  112.         l% = LEN(TEFindStr$)
  113.         CALL SELITEXT(dlogPtr&,3,0,l%)
  114.  
  115.         DO
  116.             FN frameDLOGBtn(dlogPtr&,1)
  117.             more% = 0
  118.             CALL MODALDIALOG(0,ctrlValue%)
  119.             SELECT ctrlValue%
  120.                 CASE 1
  121.                     TEFindStr$ = FN getText$(dlogPtr&,3,25)
  122.                     more% = -1
  123.                 CASE 2
  124.                     more% = -2
  125.             END SELECT
  126.         UNTIL more%
  127.         CALL DISPOSDIALOG(dlogPtr&)
  128.  
  129.  
  130.         LONG IF TEFindStr$ <> "" AND more% <> -2
  131.  
  132.             fileName$ = FILES$(1,"TEXT",,Vol%)            :' Get TEXT file to process
  133.             LONG IF fileName$ > ""
  134.                 OPEN "R",1,fileName$,0,Vol%
  135.                 workSize& = 0
  136.                 tCount% = 0
  137.                 Length& = LOF(1,1)                         :' Get length of the file to work on
  138.     
  139.                 LONG IF Length& > 32000                    :' Do we need to process multiple blocks
  140.                     Bytes& = 32000
  141.                 XELSE
  142.                     Bytes& = LOF(1,1)
  143.                 END IF
  144.     
  145.             t& = TIMER                                       :' Lets time the search
  146.                 DO
  147.                     CLEAR Bytes&,4                          :' Clear (set aside) Space _newHandle
  148.                     RECORD #1,0,workSize&                   :' Position pointer, in case multiple reads
  149.                     READ FILE #1,MEM(4+40),Bytes&           :' Block Read file section into _newHandle
  150.  
  151.                     workBytes& = Bytes&
  152.                     useBytes& = Bytes&
  153.                     work& = 0
  154.                     TEFCharPtr& = MEM(4+40)                 :' Get pointer to text
  155.                     DO
  156.                         Found% = FN TEFind(TEFCharPtr&,useBytes&,TEFindStr$)
  157.                         LONG IF Found%
  158.                             fCount% = fCount% + 1
  159.                             work& = work& + TEFSelStart + LEN(TEFindStr$)
  160.                             TEFCharPtr& = MEM(4+40) + work&   :' Get pointer to text
  161.                             useBytes& = workBytes& - work&
  162.                         END IF
  163.                     UNTIL Found% = 0
  164.  
  165.                     workSize& = workSize& + Bytes&          :' Update file pointer
  166.                     LONG IF workSize& + Bytes& > Length&
  167.                         Bytes& = Length& - workSize&         :' Bytes left to process
  168.                     END IF
  169.             UNTIL Bytes& = 0                              :' No more bytes in file to process
  170.             CLOSE #1             
  171.             CLEAR 0,4                                     :' Free up space _newHandle
  172.     
  173.             work& = TIMER - t&                            :' How long it took to scan the TEXT file
  174.             CLS                                           :' Print the Results
  175.             PRINT "  File Length:";
  176.             PRINT USING "#,###,###";Length&;
  177.             PRINT " Bytes             Scanned:";
  178.             PRINT USING "#,###,###";Length&/work&;
  179.             PRINT " bytes/second"
  180.             PRINT "Search String: ";TEFindStr$
  181.             PRINT "    File Info: ";fileName$;"   Number of Matches:";fCount%;"      Second(s):";
  182.             LONG IF work& < 1
  183.                 PRINT " < 1"
  184.             XELSE
  185.                 PRINT USING "###";work&
  186.             END IF
  187.         END IF
  188.     XELSE
  189.         done% = -1
  190.     END IF
  191.  
  192. UNTIL done%
  193.  
  194. IF ResRef% <> 0 THEN CALL CLOSERESFILE(ResRef%)     :' If in Z5, Close External Resource File
  195.  
  196. END
  197.